package com.sllp.C71S3TsllpShop.web.back;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.sllp.C71S3TsllpShop.bean.Admin;
import com.sllp.C71S3TsllpShop.bean.Result;
import com.sllp.C71S3TsllpShop.biz.impl.AdminBizImpl;
import com.sllp.C71S3TsllpShop.util.RandomUtils;
import com.sllp.C71S3TsllpShop.util.SendEmail;

@SessionAttributes({"backAddAdminCode"})
@Controller
public class BackAdminFindAction {
	
	@Resource
	private AdminBizImpl abi;
	@Resource
	private SendEmail sendEmail;

	@GetMapping("back/toAdminFind.do")
	public String toAdminFind(Model m) {
		m.addAttribute("adminList", abi.findAdminByCondition(null,1,2));
		return "back/admin-find::adminFind";
	}
	
	@ResponseBody
	@PostMapping("back/addAdmin.do")
	public Result addAdmin(Admin admin,String adminCode,@SessionAttribute("backAddAdminCode") String backAddAdminCode) {
		System.out.println(adminCode);
		System.out.println(backAddAdminCode);
		System.out.println(admin);
		if(!adminCode.equals(backAddAdminCode)) {
			return new Result(0, "验证码不正确");
		}
		admin.setLoginName(admin.getTel());
		admin.setPower(2);
		admin.setLoginState(2);
		admin.setState(2);
		admin.setImage(1);
		return abi.addAdmin(admin);
	}
	
	@Value("${mail.formMail.addr}")
	private String from;
	@Value("${mail.formMail.addAdminContent}")
	private String addAdminContent;
	
	@ResponseBody
	@PostMapping("back/sendBackAddAdminEmail.do")
	public Result sendBackAddAdminEmail(@SessionAttribute(name="backLoginAdmin",required=false) Admin admin,Model m) {
		 if(admin == null || admin.getPower() == 2) {
			 return new Result(0, "您未登录，或者权限不足");
		 }
		// 生成验证码
		String resetPwdEmailCode = RandomUtils.randomString(4);
		// 将验证码放到session中
		m.addAttribute("backAddAdminCode", resetPwdEmailCode);
		Result resultSendEmail = sendEmail.sendEmail("尤洪商城", from, admin.getEmail(), addAdminContent, resetPwdEmailCode);
		return resultSendEmail;
	}
	
	@PostMapping("back/adminFind.do")
	public String adminFind(Admin admin,Model m) {
		if(admin.getState() != null && admin.getState() == 0) {
			admin.setState(null);
		}
		m.addAttribute("adminList", abi.findAdminByCondition(admin,1,2));
		return "back/admin-find::adminShowTable";
	}
	
	@ResponseBody
	@PostMapping("back/changeAdminState.do")
	public Result changeAdminState(Admin admin,Model m) {
		return abi.updateAdminState(admin);
	}
	
	@ResponseBody
	@PostMapping("back/toShowAdminDetils.do")
	public Admin toShowAdminDetils(Admin admin) {
		return abi.findAdminByCondition(admin,null,null).get(0);
	}
	
	@PostMapping("back/adminShowToPage.do")
	public String adminShowToPage(Admin admin,int page,Model m) {
		if(admin.getState() != null && admin.getState() == 0) {
			admin.setState(null);
		}
		m.addAttribute("adminList", abi.findAdminByCondition(admin,page,2));
		return "back/admin-find::adminShowTable";
	}
	
}
